package com.parkinglot.management.controller;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.domain.AlipayTradePagePayModel;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.alipay.api.request.AlipayTradePrecreateRequest;
import com.alipay.api.response.AlipayTradePrecreateResponse;
import com.parkinglot.management.repository.entity.Account;
import com.parkinglot.management.repository.entity.Order;
import com.parkinglot.management.service.AccountService;
import com.parkinglot.management.service.impl.PayService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.math.BigDecimal;
import java.util.Objects;
import java.util.UUID;

@Api(tags = "支付管理")
@RestController
@RequestMapping("/pay")
public class PayCostController {

    @Autowired
    private PayService payService;

    @ApiOperation("根据账户余额进行支付")
    @PostMapping
    public void payByBalance(@RequestBody Order order) {
        if (Objects.isNull(order)) {
            throw new RuntimeException("订单错误");
        }
        payService.payCostByBalance(order);


    }
    @ApiOperation("根据支付包进行支付")
    @GetMapping
    public void pay(HttpServletResponse response) throws AlipayApiException, IOException {
        // 订单模型
        String productCode = "FAST_INSTANT_TRADE_PAY";
        AlipayTradePagePayModel model = new AlipayTradePagePayModel();
        model.setOutTradeNo(UUID.randomUUID().toString());

        model.setSubject("车费支付");
        model.setTotalAmount("0.01");
        model.setBody("支付测试，共0.01元");
        model.setProductCode(productCode);

        AlipayTradePagePayRequest pagePayRequest =new AlipayTradePagePayRequest();
        pagePayRequest.setReturnUrl("http://localhost:8001/order/change");
        pagePayRequest.setNotifyUrl("http://localhost:8001/order/change");
        pagePayRequest.setBizModel(model);

        AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipaydev.com/gateway.do", "2016101900722551", "MIIEvAIBADANBgkqhkiG9w0BAQEFAASC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gvIe04w9a8jSgXIedftk6hNvklW6Qs1g3UfaLfOFqYSDZ3WJ5cquLaQc3W5ulWsHNBiF7AXM3Vxq0GZlhXToBN01t+RJwH+NVQWMPqGYrYp2dTef8te9fkrcs7InXFH7/LUPhSsPS2IlmXaEz7Kr6nTnMCHN9DitWkON+q1u0ZAoGAAjgYjwiqPhWJ3g8CmVH3IO9+vHX+WIGB+/C0tgWNNflmjjMbhwXlX4AL49TR8+26UKDmcdBJM63qh/I7gDwNuj0z9BOwd7CzAdmrEsYzFMLce4O1zguY/DJ1jGgXvOEOArfCoJe5ptpni42rXsmavnCtJVBfSqwop5vBOzJGMJECgYBFnm+JsbDE5jmKt7gCpCtT65b1QQX3ftIDU+8wOux1hkzEiJual5uH9gyazinO9NHhb69taNZKHg5gEZa83Du6b7+M00DTAhG4vQSJFcu0L+0IL2i7T/7tj9v9/MbsG0N/n53sQSSJo/bvPOHxdda2cnXjFKdO7jxQ8i0kXBEhsA==", "json", "UTF-8", "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEArm8RAEuAR1DDy6KFRQ1uHV677kjnsE8hsT7GLZveTc5wCvbYdw+AqGdVmHR3uhO3ZpkporaFZLddEEOE6uQd983XOIuIq9rJQRIChgzPp5IXxB9+h4T6MRs+FE1qGowjyfhVUSrX/op6oxKl5lpcdbYemzCusm0Nu1i4hhLRKwaUFdJdSA8pgewid0tCZpCVTs0hwp9raA7nhgj0VEwojC6oU5DLtGq6w2N7jYjRFzI2wHLnkUoe2bRfuqehiAZoJnRpiNkIXPlH31Miu+ZOMHp5ujg4XnZBzrBw8svB8Jv1hP6Fox5560rcQAWPC8dXsBoF2FkPuRVpFI8+A2EGFwIDAQAB", "RSA2"); //获得初始化的AlipayClient

        String form = alipayClient.pageExecute(pagePayRequest).getBody();
        response.setContentType("text/html;charset=utf-8");
        response.getWriter().write(form);
        response.getWriter().flush();
        response.getWriter().close();
    }


}
